import random

from .base_layer import BaseLayer
from . import utils

FULL_RANDOM = False  # 通过此变量来确定信息战是完全随机，还是有一点策略性


class NetworkDefendLayer(BaseLayer):
    def __init__(self):
        self.color = None

    def setup(self, color):
        self.color = color

    def step(self):
        action_list = None
        if FULL_RANDOM:
            action_list = self.random_strategy()
        else:
            action_list = self.with_brain_strategy()
        return action_list

    def random_strategy(self):
        """
        完全随机的作战风格
        """
        has_choose = {
            201: False,
            202: False,
            203: False,
            204: False,
            205: False,
        }
        action_list = []
        # 循环执行0～4次
        for _ in range(random.randint(0, 5)):
            # 随机选择一个动作进行尝试生成
            random_number = random.randint(1, 5)
            if random_number == 1:
                # 1、尝试进行201-网络入侵扫描
                if has_choose[201] == True:
                    continue
                action = self.gen_network_attack_scan()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[201] = True
            elif random_number == 2:
                # 2、尝试进行202-病毒查杀
                if has_choose[202] == True:
                    continue
                action = self.gen_virus_scan()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[202] = True
            elif random_number == 3:
                # 3、尝试进行203-网络流量检测
                if has_choose[203] == True:
                    continue
                action = self.gen_network_flow_scan()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[203] = True
            elif random_number == 4:
                # 4、尝试进行204-更新密钥
                if has_choose[204] == True:
                    continue
                action = self.gen_update_key()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[204] = True
            elif random_number == 5:
                # 5、尝试进行205-认证强度提升
                if has_choose[205] == True:
                    continue
                action = self.gen_increase_certified_strength()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[205] = True
        return action_list

    def with_brain_strategy(self):
        """
        带点脑子的作战风格
        """
        has_choose = {
            201: False,
            202: False,
            203: False,
            204: False,
            205: False,
        }
        action_list = []
        # 循环执行0～4次
        for _ in range(random.randint(0, 5)):
            # 随机选择一个动作进行尝试生成
            random_number = random.randint(1, 5)
            if random_number == 1:
                # 1、尝试进行201-网络入侵扫描
                if has_choose[201] == True:
                    continue
                action = self.gen_network_attack_scan()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[201] = True
            elif random_number == 2:
                # 2、尝试进行202-病毒查杀
                if has_choose[202] == True:
                    continue
                action = self.gen_virus_scan()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[202] = True
            elif random_number == 3:
                # 3、尝试进行203-网络流量检测
                if has_choose[203] == True:
                    continue
                action = self.gen_network_flow_scan()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[203] = True
            elif random_number == 4:
                # 4、尝试进行204-更新密钥
                if has_choose[204] == True:
                    continue
                action = self.gen_update_key()
                if action and utils.action_valid(action):
                    action_list.append(action)
                    has_choose[204] = True
            elif random_number == 5:
                pass
        # 认证强度：对方伪装接入成功率高于阈值（0.6)
        peer_accdisguised_access_success_rete = round(utils.ACTION_SUCC_RATE[401][0], 2) if (1 - self.color) == 0 else round(
            utils.ACTION_SUCC_RATE[401][1], 2)
        if peer_accdisguised_access_success_rete >= 0.6:
            action = self.gen_increase_certified_strength()
            if action and utils.action_valid(action):
                action_list.append(action)
        return action_list

    def gen_network_attack_scan(self):
        # 生成动作
        action = {
            "color": self.color,
            "type": 201,
            "remain_time": utils.ACTION_COST_TIME[201]
        }
        return action

    def gen_virus_scan(self):
        # 生成动作
        action = {
            "color": self.color,
            "type": 202,
            "remain_time": utils.ACTION_COST_TIME[202]
        }
        return action

    def gen_network_flow_scan(self):
        # 生成动作
        action = {
            "color": self.color,
            "type": 203,
            "remain_time": utils.ACTION_COST_TIME[203]
        }
        return action

    def gen_update_key(self):
        # 1、获取我方被破解密钥且未死亡算子的id构成的列表
        has_cracked_key_ids = []
        ops_status = utils.get_ops_status(self.color)
        for op_id, op_statu in ops_status.items():
            if op_statu["has_cracked_key"] == True and op_statu["live_status"] == True:
                has_cracked_key_ids.append(op_id)
        # 2、随机取出一个活着的算子id作为目标对象，如果算子都死亡了则直接返回
        if len(has_cracked_key_ids) == 0:
            return None
        ops_id = random.choice(has_cracked_key_ids)
        # 3、生成动作
        action = {
            "color": self.color,
            "type": 204,
            "ops_id": ops_id,
            "remain_time": utils.ACTION_COST_TIME[204]
        }

    def gen_increase_certified_strength(self):
        """
        生成认证强度提升动作
        """
        # 生成动作
        action = {
            "color": self.color,
            "type": 205,
            "remain_time": utils.ACTION_COST_TIME[205]
        }
        return action

    def reset(self):
        pass
